package com.boot.project.module.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.toolkit.EncryptUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.project.core.constant.SystemConstant;
import com.boot.project.core.enums.code.impl.commonCode.ResultCode;
import com.boot.project.core.enums.code.impl.systemCode.UserCode;
import com.boot.project.core.exception.CustomerException;
import com.boot.project.core.model.system.criteria.UserCriteria;
import com.boot.project.core.model.system.dto.UserAssignDto;
import com.boot.project.core.model.system.dto.UserSaveDto;
import com.boot.project.core.model.system.entity.Role;
import com.boot.project.core.model.system.entity.User;
import com.boot.project.core.model.system.vo.UserVo;
import com.boot.project.module.system.mapper.RoleMapper;
import com.boot.project.module.system.mapper.UserMapper;
import com.boot.project.module.system.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: tsing
 * @create: 2021-01-21 22:21
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 分页条件查询，不查root
     * @param criteria 查询条件
     * @return
     */
    @Override
    public Page<UserVo> page(UserCriteria criteria) {
        Page<UserVo> pageInfo = new Page<>(criteria.getPage(), criteria.getSize());
        userMapper.pageList(pageInfo, criteria);
        return pageInfo;
    }

    /**
     * 新增用户信息
     * @param userSaveDto 用户信息
     * @param salt 加密盐
     * @param password 密码
     */
    @Override
    public void add(UserSaveDto userSaveDto, String salt, String password) {
        // 1.校验参数
        User dbUser = userMapper.findByUsername(userSaveDto.getUsername());
        if (dbUser != null) {
            throw new CustomerException(UserCode.EXISTS_USERNAME);
        }
        dbUser = userMapper.findByMobile(userSaveDto.getMobile());
        if (dbUser != null) {
            throw new CustomerException(UserCode.EXISTS_MOBILE);
        }
        // 2.封装数据
        User user = new User(userSaveDto);
        user.setSalt(salt);
        user.setPassword(password);
        // 3.进行保存
        int i = userMapper.insert(user);
        // 4.对结果进行处理
        if (i != 1) {
            throw new CustomerException(ResultCode.INSERT_FAILURE);
        }
    }

    /**
     * 根据id查询用户信息
     *
     * @param id 用户id
     * @return
     */
    @Override
    public User findOne(Long id) {
        // 1.进行查询
        User dbUser = userMapper.selectById(id);
        // 2.对结果进行判断
        if (dbUser == null) {
            throw new CustomerException(ResultCode.RETURN_DATA_NOT_FOUND);
        }
        return dbUser;
    }

    /**
     * 更新用户信息
     *
     * @param userSaveDto 用户信息
     */
    @Override
    public void edit(UserSaveDto userSaveDto) {
        // 1.校验参数
        if (userSaveDto.getId() == null) {
            throw new CustomerException(ResultCode.HANDLE_DATA_NOT_EXISTED, "更新时，用户id不能为空");
        }
        User dbUser = userMapper.selectById(userSaveDto.getId());
        if (dbUser == null) {
            throw new CustomerException(ResultCode.RETURN_DATA_NOT_FOUND, "更新时，没有找到需要更新的用户");
        }
        User tempUser = userMapper.findByUsername(userSaveDto.getUsername());
        if (tempUser != null && !tempUser.getId().equals(userSaveDto.getId())) {
            throw new CustomerException(UserCode.EXISTS_USERNAME);
        }
        tempUser = userMapper.findByMobile(userSaveDto.getMobile());
        if (tempUser != null && !tempUser.getId().equals(userSaveDto.getId())) {
            throw new CustomerException(UserCode.EXISTS_MOBILE);
        }
        // 2.封装数据
        BeanUtils.copyProperties(userSaveDto, dbUser);
        // 3.进行保存
        int i = userMapper.updateById(dbUser);
        // 4.对结果进行处理
        if (i != 1) {
            throw new CustomerException(ResultCode.UPDATE_FAILURE);
        }
    }

    /**
     * 批量删除用户信息
     *
     * @param ids 用户id的集合
     */
    @Override
    public void delete(List<Long> ids) {
        // 1.参数校验
        if (CollectionUtil.isEmpty(ids)) {
            throw new CustomerException(ResultCode.HANDLE_DATA_NOT_EXISTED, "没有需要删除的用户信息");
        }
        // 2.进行删除
        int i = userMapper.deleteBatchIds(ids);
        // 3.对结果进行处理
        if (i != ids.size()) {
            throw new CustomerException(ResultCode.DELETE_FAILURE);
        }
    }

    /**
     * 设置用户角色
     *
     * @param dto 设置的信息
     */
    @Override
    public void assignRole(UserAssignDto dto) {
        // 1.校验参数
        if (dto.getUserId() == null) {
            throw new CustomerException(ResultCode.HANDLE_DATA_NOT_EXISTED, "分配角色时，用户id不能为空");
        }
        User dbUser = userMapper.selectById(dto.getUserId());
        if (dbUser == null) {
            throw new CustomerException(ResultCode.RETURN_DATA_NOT_FOUND, "分配角色时，没有找到对应的用户信息");
        }
        if (dto.getRoleId() == null) {
            throw new CustomerException(ResultCode.HANDLE_DATA_NOT_EXISTED, "分配角色时，角色id不能为空");
        }
        Role dbRole = roleMapper.selectById(dto.getRoleId());
        if (dbRole == null) {
            throw new CustomerException(ResultCode.RETURN_DATA_NOT_FOUND, "分配角色时，没有找到对应的角色信息");
        }
        // 2.封装数据
        dbUser.setRoleId(dto.getRoleId());
        // 3.进行保存
        int i = userMapper.updateById(dbUser);
        // 4.对结果进行处理
        if (i != 1) {
            throw new CustomerException(UserCode.ASSIGN_ROLE_FAILURE);
        }
    }
}
